<?php

use app\common\service\ConfigService;
use think\facade\Config;

require __DIR__ . '/vendor/autoload.php';

// 初始化应用
$app = new \think\App();
$app->initialize();

try {
    // 生成随机手机号
    $mobile = '138' . str_pad(mt_rand(0, 9999999), 8, '0', STR_PAD_LEFT);
    
    // 获取最大的sn号
    $maxSn = \think\facade\Db::name('user')->max('sn');
    $newSn = $maxSn ? $maxSn + 1 : 1000001; // 如果没有记录，从1000001开始
    $passwordSalt = Config::get('project.unique_identification');
    $password = create_password('123456', $passwordSalt);
    $avatar = ConfigService::get('default_image', 'user_avatar');

    // 准备用户数据
    $userData = [
        'sn' => $newSn,  // 自增的编号
        'avatar' => $avatar,  // 默认头像
        'real_name' => '测试用户',
        'nickname' => '游戏玩家' . mt_rand(1000, 9999),
        'mobile' => $mobile,
        'password' => $password, // 默认密码123456
        'sex' => 0,
        'channel' => 0,
        'is_disable' => 0,
        'create_time' => time(),
        'update_time' => time()
    ];

    // 插入用户数据
    $userId = \think\facade\Db::name('user')->insertGetId($userData);

    if ($userId) {
        echo "用户创建成功！\n";
        echo "手机号: {$mobile}\n";
        echo "密码: 123456\n";
        echo "用户ID: {$userId}\n";
        echo "用户编号: {$newSn}\n";
        echo "昵称: {$userData['nickname']}\n";
        
        // 直接获取token
        $url = 'http://localhost/api/login/account';
        $loginData = [
            'account' => $mobile,
            'password' => '123456'
        ];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($loginData));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json'
        ]);

        $response = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($response, true);

        if ($result['code'] === 1) {
            echo "\n登录成功！\n";
            echo "Token: " . $result['data']['token'] . "\n";
            echo "用户信息：\n";
            echo json_encode($result['data']['user_info'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
        }
    }

} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
} 